package org.leon.controller;

import org.leon.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/27.
 */
@Controller
@RequestMapping(value = "/interceptor")
public class UserInterceptorController {

    @RequestMapping(value = "/loginForm")
    public String loginForm(){
        return "interceptor/loginForm";
    }

    @RequestMapping(value = "/login")
    public ModelAndView login(
            String loginname, String password,
            ModelAndView mv,
            HttpSession session){
            if(loginname!=null && loginname.equals("hgd")
                    && password!=null && password.equals("123")){
                User user = new User();
                user.setLoginname(loginname);
                user.setUsername("管理员");
                session.setAttribute("user", user);
                mv.setViewName("redirect:main");
            }else{
                mv.addObject("message","用户名或者密码错误，请重新登录！");
                mv.setViewName("interceptor/loginForm");
            }
            return mv;
    }


    @RequestMapping(value = "/main")
    public String main(Model model){
        List<Book> bookList = new ArrayList<>();
        bookList.add(new Book(1, "Python","Mike"));
        bookList.add(new Book(2, "Java","James"));
        model.addAttribute("bookList", bookList);
        return "interceptor/main";
    }

}
